import React, { useEffect, useState } from 'react'
import PropTypes from 'prop-types'
import { message } from 'antd'
import { mapStateToProps, mapDispatchToProps } from '@components/Component'
import { connect } from 'react-redux'
import DetailInfo from './components/DetailInfo'
import { getDetail } from './ajax/indexAjax'
import { CaseContext } from './context'
import { awaitWrap } from '@js/tool'
// import OperateTags from './components/OperateTags'
import style from './Index.module.less'

// 获取案件详情数据
async function getCaseDetailData (id) {
  if (!id) {
    return message.warning('案号不存在')
  }
  const hide = message.loading('加载中', 120)
  const [e, d] = await awaitWrap(getDetail(id))
  hide()
  if (e) {
    message.warning(e.message)
    return false
  }
  // if (d.procedure === '普通程序') {
  //   d.procedure = '非简易程序'
  // }
  return d
}

// 完善信息
function modifyInfo (delWindow, addWindow, id, data = {}, type) {
  delWindow(id)
  addWindow({ id: `CaseDetail-${id}`, title: `${type}信息-${data.accept_no || data.serial_no}`, componentName: 'AcceptCase', forceRefresh: true, meta: { id: data.id, editable: true } })
}

// 初始化数据
async function initData (meta, setCaseDetail) {
  const d = await getCaseDetailData(meta.id)
  if (d) {
    setCaseDetail(d)
    // setStatus(d.status)
  }
}

function Main ({ meta, delWindow, addWindow, id }) {
  const [caseDetail, setCaseDetail] = useState(null)
  // const [status, setStatus] = useState('')
  useEffect(() => {
    initData(meta, setCaseDetail)
  }, [meta])
  const value = { caseDetail }
  const fullInfo = modifyInfo.bind(this, delWindow, addWindow)
  return (
    <CaseContext.Provider value={value}>
      <div className={style.container}>
        <DetailInfo caseDetail={caseDetail} fullInfo={(type) => fullInfo(id, caseDetail, type)} refreshFn={() => { initData(meta, setCaseDetail) }} />
        {/* <OperateTags id={meta.id} meta={meta} caseDetail={caseDetail} status={status} editable refreshCaseData={() => initData(meta, setCaseDetail, setStatus)} fullInfo={() => fullInfo(id, caseDetail, '完善')} /> */}
      </div>
    </CaseContext.Provider>
  )
}

Main.propTypes = {
  meta: PropTypes.object
}

Main.defaultProps = {
  meta: {}
}

const mapState = mapStateToProps(function (state) {
  return {
  }
})

const mapDispatch = mapDispatchToProps(function (dispatch) {
  return {
  }
})

export default connect(mapState, mapDispatch)(Main)
